//
//  ViewController.m
//  04-UIControl
//
//  Created by 冀永金 on 16/7/19.
//  Copyright © 2016年 冀永金. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UISwitch *switchControl;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view .backgroundColor = [UIColor grayColor];
    self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 250, 200, 300)];
    [self.view addSubview:_imageView];
    _imageView.image = [UIImage imageNamed:@"eat_0.jpg"];
    //UISwitch
    self.switchControl = [[UISwitch alloc]initWithFrame:CGRectMake(100, 100, 200, 50)];
    [self.view addSubview:_switchControl];
    // 1.开启时的控件内部颜色
    _switchControl.onTintColor = [UIColor redColor];
    // 2.边框及切换到关闭时的颜色(null_resettable)
    //switchControl.tintColor = [UIColor cyanColor];
   _switchControl.thumbTintColor = [UIColor blackColor];
    // 打开时的图片 iOS7失效
    _switchControl.onImage = [UIImage imageNamed:@"2 下午10.34.15.png"];
    // offImage  关闭的图片   iOS7失效
  // _switchControl.on = YES;
    NSLog(@"%s",_switchControl.isOn ? "YES" : "ON");;
    //[_switchControl setOn:YES animated:YES];
    [_switchControl addTarget:self action:@selector(handleSwitch:) forControlEvents:UIControlEventValueChanged];
    
    
    UISlider *slider = [[UISlider alloc]initWithFrame:CGRectMake(50, 150, 300, 20)];
    [self.view addSubview:slider];
    //
    slider.continuous = YES;
    //1.当前滑块的值
    slider.value = .5;
    //2/滑块最小值 默认0
    slider.minimumValue = 1.0f;
    // 滑块最大值 默认1
    slider.maximumValue = 10.0f;
    // 最大最小值那边显示的图片
    //slider.minimumValueImage
    //slider.maximumValueImage
    // 划过的部分的颜色
    //slider.minimumTrackTintColor = [UIColor greenColor];
    // 为划过部分的颜色
   // slider.maximumTrackTintColor = [UIColor redColor];
    [slider addTarget:self action:@selector(handleSlider:) forControlEvents:UIControlEventValueChanged];
    // 自定义slider
    [slider setMaximumTrackImage:[UIImage imageNamed:@"player_slider_playback_right@2x.png"] forState:UIControlStateNormal];
    [slider setMinimumTrackImage:[UIImage imageNamed:@"player_slider_playback_left@2x.png"] forState:UIControlStateNormal];
    [slider setThumbImage:[UIImage imageNamed:@"player_slider_playback_thumb@2x.png"] forState:UIControlStateNormal];
    
    
    
    UISegmentedControl *seg = [[UISegmentedControl alloc]initWithItems:@[@"1",@"2",@"3"]];
    [self.view addSubview:seg];
    seg.tag = 1000;
    seg.frame = CGRectMake(0, 0, 300, 80);
    // 默认为NO
   // seg.momentary = YES;
    NSInteger numberOfSegments = seg.numberOfSegments;
    NSLog(@"分段控制器的Item个数：%lu",numberOfSegments);
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:button];
    button.backgroundColor = [UIColor blueColor];
    button.frame = CGRectMake(20, 100, 100, 50);
    [button addTarget:self action:@selector(addSegment:) forControlEvents:UIControlEventTouchUpInside];
    // 设置偏移量
    [seg setContentOffset:CGSizeMake(0, 20) forSegmentAtIndex:0];
    // 被选中的下标
    seg.selectedSegmentIndex = 0;
    // 文字及其边框颜色
    seg.tintColor = [UIColor redColor];
    
    [seg addTarget:self action:@selector(handleChangeColor:) forControlEvents:UIControlEventValueChanged];
    // Do any additional setup after loading the view, typically from a nib.
}
- (void)handleChangeColor:(UISegmentedControl *)seg {
    if (seg.selectedSegmentIndex == 0) {
        NSLog(@"00");
    }else if (seg.selectedSegmentIndex == 1) {
        NSLog(@"11");
    }else {
        NSLog(@"22");
    }
}
- (void)addSegment:(UIButton *)button {
    UISegmentedControl *segment = [self.view viewWithTag:1000];
    [segment insertSegmentWithTitle:@"0" atIndex:0 animated:YES];
}
- (void)handleSwitch:(UISwitch *)swi {
    NSMutableArray *marr = [NSMutableArray array];
    if (swi.on == YES) {
        for (int i = 0; i < 40; i ++) {
            NSString *name = [NSString stringWithFormat:@"eat_%d.jpg",i];
            UIImage *image = [UIImage imageNamed:name];
            [marr addObject:image];
            //NSLog(@"%@",marr);
        }
        self.imageView.animationImages = marr;
        self.imageView.animationDuration = 5;
        self.imageView.animationRepeatCount = 0;
        [self.imageView startAnimating];
    }else {
        [self.imageView stopAnimating];
        //self.imageView = nil;
    }
    
}
- (void)handleSlider :(UISlider *)sli {
    if (self.switchControl.on == YES) {
        self.imageView.animationDuration = 10 - sli.value;
        [self.imageView startAnimating];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
